package com.shj.em.aa.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.shj.em.aa.dto.RoleSearchDTO;
import com.shj.em.aa.dto.UserSearchDTO;
import com.shj.em.aa.entity.Role;
import com.shj.em.aa.entity.User;
import com.shj.em.aa.service.UserService;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;
import com.shj.em.common.annotation.Log;
import com.shj.em.util.ShiroUtils;
import com.shj.em.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/aa/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/list")
    public PageVO<User> list(@RequestBody UserSearchDTO searchDTO){
        Page<User> page = userService.queryUserPage(searchDTO);
        PageVO<User> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @RequestMapping("/changepassword")
    @ResponseBody
    ResMsg changepassword(String userId, String password, String oldPwd) {
        try{
            if(StringUtils.isEmpty(oldPwd)){
                return ResMsg.error("旧密码不能为空。");
            }
            userService.changePwd(ShiroUtils.getUserId(), password, oldPwd);
            return ResMsg.ok();
        }catch (Exception e){
            return ResMsg.error(e.getMessage());
        }
    }


    @RequestMapping("/resetPwd")
    @ResponseBody
    ResMsg resetPwd(String userId) {
        String msg = userService.resetPwd(userId);
        if(StringUtils.isBlank(msg)){
            return ResMsg.ok();
        }
        return ResMsg.error(msg);
    }

    @Log("重置所有密码")
    @RequestMapping("/resetAllPwd")
    @ResponseBody
    ResMsg resetAllPwd(String userId, String password, String oldPwd) {
        try{
            userService.resetAllPwd();
            return ResMsg.ok();
        }catch (Exception e){
            return ResMsg.error(e.getMessage());
        }
    }
}
